package com.dgut.service.background.service;

import com.dgut.service.background.dao.CheckDao;
import com.dgut.service.background.dao.InspectorDao;
import com.dgut.service.background.entity.Inspector;
import com.dgut.service.background.entity.TransInspector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CheckService {
    @Autowired
    private CheckDao checkDao;

    @Autowired
    private InspectorDao inspectorDao;

    public Page<TransInspector> getAllCheckData(int page){
        Pageable pageable = PageRequest.of(page, 10);
        Page<TransInspector> all = checkDao.findAllByCheckedLike("0", pageable);
        return all;
    }

    public void approvedCheck(int transId){
        TransInspector transInspector = checkDao.findByTransId(transId);
        Inspector i = inspectorDao.findInspectorByOpenid(transInspector.getOpenid());
        if (i == null){
            Inspector inspector = new Inspector();
            inspector.setOpenid(transInspector.getOpenid());
            inspector.setName(transInspector.getName());
            inspector.setPhone(transInspector.getPhone());
            inspector.setSex(transInspector.getSex());
            inspector.setPassword(transInspector.getPassword());
            inspectorDao.save(inspector);
        }
        transInspector.setChecked("1");
        checkDao.save(transInspector);
    }

    public void refusedCheck(int transId){
        TransInspector transInspector = checkDao.findByTransId(transId);
        transInspector.setChecked("2");
        checkDao.save(transInspector);
    }
}
